package com.aurora_manito.xiaomi.Controller;


import com.aurora_manito.xiaomi.entity.*;
import com.aurora_manito.xiaomi.service.AddressService;
import com.aurora_manito.xiaomi.service.CartService;
import com.aurora_manito.xiaomi.service.OrderService;
import com.aurora_manito.xiaomi.utils.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import javax.swing.text.Caret;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Aurora_Manito
 * @since 2020-05-27
 */
@RestController
@RequestMapping("/xiaomi/order")
public class OrderController {
    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private AddressService addressService;
    @GetMapping(value = "/getOrderView", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView clearCartAjax(HttpSession session) {
        ModelAndView modelAndView=new ModelAndView("forward:/order.jsp");
        //1.判断是否有登录
        User user= (User) session.getAttribute("user");
        //判断是否登录
        if(user==null)
        {
            return new ModelAndView("redirect:/login.jsp");
        }
        //2.查询购买的商品
        List<Cart> carts=cartService.findByUid(user.getId());
        modelAndView.addObject("carts",carts);
        //3.获取收货地址
        List<Address>  addList=addressService.findByUid(user.getId());
        modelAndView.addObject("addList",addList);
        return modelAndView;
    }

    @GetMapping(value = "/addOrder", produces = {"application/json;charset=UTF-8"})
    @ResponseBody //序列化--》类型转换  --》jackson ---Json
    public ModelAndView addOrder(HttpSession session,String aid)
    {
        ModelAndView modelAndView=new ModelAndView("forward:/orderSuccess.jsp");
        //1.判断是否有登录
        User user= (User) session.getAttribute("user");
        //判断是否登录
        if(user==null)
        {
            return new ModelAndView("redirect:/login.jsp");
        }
        //获取购买的东西
        List<Cart> carts = cartService.findByUid(user.getId());
        //如果购物车为空订单生成失败
        if(carts.size()==0||carts==null)
        {
            modelAndView.setViewName("redirect:/message.jsp");
            modelAndView.addObject("msg","购物车为空");
            return modelAndView;
        }
        //2.创建订单号
        String oid= RandomUtils.createOrderId();

        //2.创建订单详情
        List<Orderdetail> orderdetailList=new ArrayList<>();
        //算总钱数 2020 6 5
        int totalMoney=0;
        for (Cart cart : carts) {
            Orderdetail orderdetail=new Orderdetail(null,oid,cart.getGoodsId(),cart.getBuyCount(),cart.getSubtotal());
            orderdetailList.add(orderdetail);
            totalMoney+=cart.getSubtotal();
        }
        //Status 1.下订单未付款。2, 付款未发货 3.发货 4 订单完成
        Order order=new Order(oid,user.getId(),totalMoney,"1",new Date(),Integer.parseInt(aid));
        int num=orderService.saveOrder(orderdetailList,order);
        if(num==0)
        {
            modelAndView.setViewName("redirect:/message.jsp");
            modelAndView.addObject("msg","订单生成失败");
            return modelAndView;
        }
        modelAndView.addObject("order",order);
        return modelAndView;

    }
}

